//Source file: D:\\test\\com\\ce\\pms\\custservice\\model\\DispatchFormDO.java

package com.ce.pms.custservice.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.ce.pms.common.model.HmDeptDomain;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.pms.system.custservice.model.EmergencyDegreeDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.model.IDomainObject;

/**
 * 实体名称:派工单 关联数据表:CE_DISPATCH_FORM
 *
 * @author yandawei
 * @CreateDate:2008-12-8
 */
@Entity
@Table(name = "CE_DISPATCH_FORM")
public class DispatchFormDO implements IDomainObject {
	/**
	 *
	 */
	private static final long serialVersionUID = 380151359377621409L;

	/**
	 * 派工单状态:001待处理
	 */
	public static final String DISPATCH_STATUS_NEW_CODE = "001";

	/**
	 * 派工单状态:002处理中
	 */
	public static final String DISPATCH_STATUS_HANDLING_CODE = "002";

	/**
	 * 派工单状态:003完工
	 */
	public static final String DISPATCH_STATUS_COMPLETE_CODE = "003";

	/**
	 * 派工单状态:000作废
	 */
	public static final String DISPATCH_STATUS_CANCLE_CODE = "000";

	/**
	 * 派工单ID
	 */
	@Id
	@GeneratedValue
	@Column(name = "DISPATCH_FORM_ID")
	private Long keyword;

	/**
	 * 派工单编号
	 */
	@Column(name = "DISPATCH_NO", nullable = false, length = 20)
	private String dispatchNo;

	/**
	 * 派单人
	 */
	@Column(name = "DISPATCH_MAN", length = 36)
	private String dispatchMan;

	/**
	 * 派单时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "DISPATCH_TIME")
	private Date dispatchTime;

	/**
	 * 预约时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "APPOINTMENT")
	private Date appointment;

	/**
	 * 接单人
	 */
	@Column(name = "RECEIVER", length = 36)
	private String receiver;

	/**
	 * 服务内容
	 */
	@Column(name = "SERVICE_CONTENT", nullable = false, length = 200)
	private String serviceContent;

	/**
	 * 派工单状态:001新建;002处理中;003完工;000作废
	 */
	@Column(name = "STATUZ", length = 3)
	private String statuz;

	/**
	 * 紧急程度
	 */
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "EMERGENCY_DEGREE_ID")
	private EmergencyDegreeDO emergencyDegreeDO;

	/**
	 * 房号
	 */
	@Column(name = "HOUSE_NUMBER", length = 36)
	private String houseNumber;

	/**
	 * 房屋ID
	 */
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "HOUSE_ID")
	private HouseDomain house;

	/**
	 * 联系人
	 */
	@Column(name = "CONTACT", length = 36)
	private String contact;

	/**
	 * 联系电话
	 */
	@Column(name = "LINK_PHONE", length = 36)
	private String linkPhone;

	/**
	 * 开工时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "START_TIME")
	private Date startTime;

	/**
	 * 完工时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "END_TIME")
	private Date endTime;

	/**
	 * 备注
	 */
	@Column(name = "REMARK", length = 200)
	private String remark;

	/**
	 * 服务项目
	 */
	@org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "dispatchFormDO", cascade = { CascadeType.ALL })
	private Set<AbstractServiceItem> abstractServiceItemSet = new HashSet<AbstractServiceItem>();

	/**
	 * 接单部门
	 */
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEPT_ID")
	private HmDeptDomain deptDO;

	/**
	 * 提交任务
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DISPATCH_EVENT_ID")
	private SubmitTaskDO submitTaskDO;

	/**
	 * 反馈登记
	 */
	@org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "dispatchForm", cascade = { CascadeType.ALL })
	private Set<FeedbackRecord> feedbackRecordSet = new HashSet<FeedbackRecord>();

	/**
	 * 接单登记
	 */
	@Embedded
	private Dispatching dispatching;

	/**
	 * 系统信息
	 */
	@Embedded
	private SysInfo sysInfo;

	/**
	 * 默认构造器
	 *
	 * @return com.ce.pms.custservice.model.DispatchFormDO
	 * @roseuid 493627AC0213
	 */
	public DispatchFormDO() {
		initStatuz();
	}

	/**
	 * 自定义构造器
	 *
	 * @param keyword -
	 *            派工单ID
	 * @return com.ce.pms.custservice.model.DispatchFormDO
	 * @roseuid 4923E1D80251
	 */
	public DispatchFormDO(Long keyword) {
		this.keyword = keyword;
		initStatuz();
	}

	/**
	 * 设置状态"新建" statuz=001
	 *
	 * @roseuid 4923E28A0000
	 */
	private void initStatuz() {
		setStatuz(DISPATCH_STATUS_NEW_CODE);
	}

	/**
	 * 设置状态"处理中" statuz=002
	 *
	 * @roseuid 4923E362036B
	 */
	public void setStatuzProcessing() {
		setStatuz(DISPATCH_STATUS_HANDLING_CODE);
	}

	/**
	 * 设置状态"已完成" statuz=003
	 *
	 * @roseuid 4923E46301E4
	 */
	public void setStatuzComplete() {
		setStatuz(DISPATCH_STATUS_COMPLETE_CODE);
	}

	/***************************************************************************
	 * 功能描述: 初始化派工单编号<br>
	 *
	 * @param dispatchNo
	 *            Create author:yandawei <br>
	 *            Create on:2008-12-11 <br>
	 *            Edit author: <br>
	 *            Edit on: <br>
	 *            Why: <br>
	 */
	public void initDispatchFormNo(String dispatchNo) {
		setDispatchNo(dispatchNo);
	}

	/***************************************************************************
	 * 功能描述: 设置提交任务<br>
	 *
	 * @param submitTaskDO
	 *            Create author:yandawei <br>
	 *            Create on:2008-12-11 <br>
	 *            Edit author: <br>
	 *            Edit on: <br>
	 *            Why: <br>
	 */
	public void initSubmitTask(SubmitTaskDO submitTaskDO) {
		setSubmitTaskDO(submitTaskDO);
	}

	/***************************************************************************
	 * 功能描述: 登记接单信息 <br>
	 *
	 * @param dispatching
	 *            Create author:yandawei <br>
	 *            Create on:2008-12-11 <br>
	 *            Edit author: <br>
	 *            Edit on: <br>
	 *            Why: <br>
	 */
	public void recordDispatching(Dispatching dispatching) {
		setDispatching(dispatching);
	}

	/**
	 * 设置"状态"作废 statuz=000
	 *
	 * @roseuid 492528D403D8
	 */
	public void setStatuzLogicDelete() {
		setStatuz(DISPATCH_STATUS_CANCLE_CODE);
	}

	/**
	 * 功能描述: 判断派工单是否完工1.当statuz=003时,返回ture;否则,返回flase<br>
	 *
	 * @return Create author:yandawei <br>
	 *         Create on:2008-12-16 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	public boolean isDFormComplete() {
		if (DISPATCH_STATUS_COMPLETE_CODE.equals(getStatuz())) {
			return true;
		}
		return false;
	}

	/**
	 * 新增派工单反馈登记
	 *
	 * @param feedback
	 * @roseuid 49241208005D
	 */
	public void addFeedback(FeedbackRecord feedback) {
		this.getFeedbackRecordSet().add(feedback);
	}

	/**
	 * 替换派工单反馈
	 *
	 * @param feeback
	 * @roseuid 49363614033C
	 */
	public void replaceFeedback(FeedbackRecord feeback) {
		for (Iterator it = this.getFeedbackRecordSet().iterator(); it.hasNext();) {
			FeedbackRecord oldFeeback = (FeedbackRecord) it.next();
			if (oldFeeback.getKeyword().equals(feeback.getKeyword())) {
				this.getFeedbackRecordSet().remove(oldFeeback);
				this.getFeedbackRecordSet().add(feeback);
				break;
			}
		}
	}

	/**
	 * 添加服务项目
	 *
	 * @param servItem -
	 *            服务项目
	 * @roseuid 493388490242
	 */
	public void addServItem(AbstractServiceItem servItem) {
		this.getAbstractServiceItemSet().add(servItem);
	}

	/**
	 * 删除服务项目
	 *
	 * @param servItem -
	 *            服务项目
	 * @roseuid 493388490261
	 */
	public void removeServItem(AbstractServiceItem servItem) {
		for (Iterator it = this.getAbstractServiceItemSet().iterator(); it
				.hasNext();) {
			AbstractServiceItem oldServItem = (AbstractServiceItem) it.next();
			if (oldServItem.getKeyword().equals(servItem.getKeyword())) {
				this.getAbstractServiceItemSet().remove(oldServItem);
				break;
			}
		}
	}

	/**
	 * 编辑服务项目
	 *
	 * @param servItem -
	 *            服务项目
	 * @roseuid 49338849029F
	 */
	public void replaceServItem(AbstractServiceItem servItem) {
		for (Iterator it = this.getAbstractServiceItemSet().iterator(); it
				.hasNext();) {
			AbstractServiceItem oldServItem = (AbstractServiceItem) it.next();
			if (oldServItem.getKeyword().equals(servItem.getKeyword())) {
				this.getAbstractServiceItemSet().remove(oldServItem);
				this.getAbstractServiceItemSet().add(servItem);
				break;
			}
		}
	}

	/**
	 * 功能描述:  是否允许作废派工单. 1. 当派工单状态为新建statuz=001时,返回true;否则,返回false <br>
	 * @roseuid 492A4BBB034B
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkAllowSetAsideDForm() {
		if (DISPATCH_STATUS_NEW_CODE.equals(getStatuz())) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 功能描述: 检查必须字段 <br>
	 *
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void checkRequiredField(){
		if (this.deptDO==null
				|| this.submitTaskDO == null
				|| BlankUtil.isBlank(this.dispatchNo)
				|| BlankUtil.isBlank(this.serviceContent)
				|| this.emergencyDegreeDO == null
				|| this.sysInfo==null
				|| this.sysInfo.getCreator() == null
				|| this.sysInfo.getSysDate() == null) {
			throw new BusinessException("common:mustFill_null_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 是否允许反馈登记作废 当派工单状态为"处理中"statuz=002  or "待处理"statuz=001时.返回true,否则返回false
	 * statuz=000时返回false
	 * @roseuid 492D1842000F
	 */
	public boolean isAllowSetAsideFeedback() {
		if (DISPATCH_STATUS_NEW_CODE.equals(getStatuz())
				|| DISPATCH_STATUS_HANDLING_CODE.equals(getStatuz())) {
			return true;
		}
		return false;
	}

	public Date getAppointment() {
		return appointment;
	}

	public void setAppointment(Date appointment) {
		this.appointment = appointment;
	}

	public String getDispatchMan() {
		return dispatchMan;
	}

	public void setDispatchMan(String dispatchMan) {
		this.dispatchMan = dispatchMan;
	}

	public Date getDispatchTime() {
		return dispatchTime;
	}

	public void setDispatchTime(Date dispatchTime) {
		this.dispatchTime = dispatchTime;
	}

	public Long getKeyword() {
		return keyword;
	}

	public void setKeyword(Long keyword) {
		this.keyword = keyword;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getServiceContent() {
		return serviceContent;
	}

	public void setServiceContent(String serviceContent) {
		this.serviceContent = serviceContent;
	}

	public String getStatuz() {
		return statuz;
	}

	public void setStatuz(String statuz) {
		this.statuz = statuz;
	}

	public SysInfo getSysInfo() {
		return sysInfo;
	}

	public void setSysInfo(SysInfo sysInfo) {
		this.sysInfo = sysInfo;
	}

	public Set<AbstractServiceItem> getAbstractServiceItemSet() {
		return abstractServiceItemSet;
	}

	public void setAbstractServiceItemSet(
			Set<AbstractServiceItem> abstractServiceItemSet) {
		this.abstractServiceItemSet = abstractServiceItemSet;
	}

	public HmDeptDomain getDeptDO() {
		return deptDO;
	}

	public void setDeptDO(HmDeptDomain deptDO) {
		this.deptDO = deptDO;
	}

	public EmergencyDegreeDO getEmergencyDegreeDO() {
		return emergencyDegreeDO;
	}

	public void setEmergencyDegreeDO(EmergencyDegreeDO emergencyDegreeDO) {
		this.emergencyDegreeDO = emergencyDegreeDO;
	}

	public Set<FeedbackRecord> getFeedbackRecordSet() {
		return feedbackRecordSet;
	}

	public void setFeedbackRecordSet(Set<FeedbackRecord> feedbackRecordSet) {
		this.feedbackRecordSet = feedbackRecordSet;
	}

	public SubmitTaskDO getSubmitTaskDO() {
		return submitTaskDO;
	}

	public void setSubmitTaskDO(SubmitTaskDO submitTaskDO) {
		this.submitTaskDO = submitTaskDO;
	}

	public Dispatching getDispatching() {
		return dispatching;
	}

	public void setDispatching(Dispatching dispatching) {
		this.dispatching = dispatching;
	}

	public String getDispatchNo() {
		return dispatchNo;
	}

	public void setDispatchNo(String dispatchNo) {
		this.dispatchNo = dispatchNo;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getLinkPhone() {
		return linkPhone;
	}

	public void setLinkPhone(String linkPhone) {
		this.linkPhone = linkPhone;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getHouseNumber() {
		return houseNumber;
	}

	public void setHouseNumber(String houseNumber) {
		this.houseNumber = houseNumber;
	}

	public HouseDomain getHouse() {
		return house;
	}

	public void setHouse(HouseDomain house) {
		this.house = house;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	/**
	 * 功能描述: 是否允许添加账单 <br>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean isAllowAddBill(){
		if (SysrefcodeDO.CE_DISPATCH_FORM_STATUZ_CANCEL.equals(this.statuz)
				|| this.isDFormComplete()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 功能描述: 是否允许添加反馈 <br>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean isAllowAddFeedback(){
		if (DISPATCH_STATUS_HANDLING_CODE.equals(this.statuz)) {
			return true;
		}
		return false;
	}

	/**
	 * 功能描述: 是否允许接单登记<br>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean isAllowDispatching(){
		if (SysrefcodeDO.CE_DISPATCH_FORM_STATUZ_CANCEL.equals(this.statuz)
				|| this.isDFormComplete()) {
			return false;
		}
		return true;
	}
}
